/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.editor.message;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.createUniqueName;
import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getGates;

import org.eclipse.uml2.uml.Gate;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.diagram.sequence.sedi.model.command.MessageEndPosition;

/**
 * Assists in handling of messages on Interactions. Instances of this class is created by
 * calling {@link MessageHelper#select(Object)}.
 * 
 * @author frankd
 */
class InteractionMessageHelper extends GateMessageHelper {

    @Override
    public Interaction getInteraction(MessageEndPosition position) {
        return (Interaction) position.getOwner();
    }

    @Override
    public MessageEnd createMessageEnd(MessageEndPosition position, boolean send) {
        MessageEnd end = UMLFactory.eINSTANCE.createGate();
        addMessageEnd(position, end, send);
        return end;
    }

    @Override
    public void addMessageEnd(final MessageEndPosition position, final MessageEnd end, boolean send) {
        Gate gate = (Gate) end;
        final Interaction interaction = getInteraction(position);
        gate.setName(createUniqueName(getGates(interaction), "Gate_"));
        getGates(interaction).add(gate);
    }

    @Override
    public MessageEndPosition removeMessageEnd(final MessageEnd end) {
        Interaction interaction = (Interaction) end.getOwner();
        interaction.getFormalGates().remove(end);
        return new MessageEndPosition(interaction);
    }
}